/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/9
 * Time: 19:40
 */
public class Solution {
    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int desc = -1;
        //1.找到最后一个需要复写的数
        while(true){
            if(arr[cur] == 0){
                desc += 2;
            }else{
                desc += 1;
            }
            if(desc >= arr.length - 1){
                break;
            }
            cur++;
        }
        //2.处理边界情况
        if(desc == arr.length){
            arr[arr.length - 1] = 0;
            desc -= 2;
            cur--;
        }
        //3.从后往前完成复写操作
        while(desc > -1){
            if(arr[cur] == 0){
                arr[desc--] = arr[cur];
            }
            arr[desc--] = arr[cur];
            cur--;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        s.duplicateZeros(new int[]{1,2,3,4,5,6,7,8,9});
    }
}